const mongoose = require("mongoose");

module.exports.getOrder3 = async (inData) => {
    let { searchType, keywords, pageLimit, currentPage } = inData;
    keywords = new RegExp(keywords, 'i');
    let queryContent = { [searchType]: keywords };
    if (searchType === "notype") {
        queryContent = {};
    }
    const count = await mongoose.model("orderModel").find().populate({ path: 'userid', select: 'name' }).populate({ path: 'carid', populate: { path: 'ownerid', select: 'name' } }).countDocuments(queryContent);
    const list = await mongoose.model("orderModel").find().populate({ path: 'userid', select: 'name' }).populate({ path: 'carid', populate: { path: 'ownerid', select: 'name' } }).find(queryContent).skip((currentPage - 1) * ~~pageLimit).limit(~~pageLimit)
    return { count, list };
}

module.exports.createOrder3 = async (inData) => {
    const outData = await mongoose.model("orderModel").create(inData);
    return outData;
}

module.exports.updateOrder3 = async (inData) => {
    const { query, updateData } = inData;
    const outData = await mongoose.model("orderModel").updateOne(query, updateData);
    return outData;
}

module.exports.deleteOrder3 = async (inData) => {
    const outData = await mongoose.model("orderModel").deleteOne(inData);
    return outData;
}